﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UniRlv.COL;
using UniRlv.UI.Main.ManufactureManage;
using UniRlv.Utility;
using UniRlv.Utility.Extend;

namespace UniRlv.UI.Main.BasicInfo
{
    public partial class FrmPrintInfo : FrmBase
    {
        BindingList<PrintInfo> details = new BindingList<PrintInfo>();
        PrintInfo curPrintInfo = null;
        private int nPrintLackQty;

        public FrmPrintInfo()
        {
            InitializeComponent();
        }

        private void FrmPrintInfo_Load(object sender, EventArgs e)
        {
            #region 初始化模板列表

            Wait();
            string msg = string.Empty;
            List<PrintInfo> liPrintInfos = DALOpr.Query<PrintInfo>("taskid=0", ref msg);

            if (!string.IsNullOrEmpty(msg))
            {
                ShowErrorMessageBox("出现错误", msg);
            }
            else
            {
                if (liPrintInfos == null || liPrintInfos.Count == 0)
                {
                    //ShowWarnningMessageBox("提示", "缺少打印模板信息");
                }
                else
                {
                    foreach (var item in liPrintInfos)
                    {
                        details.Add(item);
                    }
                }

            }

            WaitBack();

            #endregion


            #region 初始化工具栏

            toolBtnModify.Visible = true;
            toolBtnAdd.Visible = true;
            toolBtnDelete.Visible = true;

            toolBtnAdd.Click += toolBtnAdd_Click;
            toolBtnDelete.Click += toolBtnDelete_Click;
            toolBtnModify.Click += toolBtnModify_Click;

            #endregion

            UIHelper.InitDataGridView(dgvDetails);
            dgvDetails.DataSource = details;

            nPrintLackQty = ConfigurationHelper.AppGetInt("printLackQty");

            if (nPrintLackQty <= 0)
            {
                nPrintLackQty = 50;
            }
        }

        //private void toolBtnModify_Click(object sender, EventArgs e)
        //{
        //    if (dgvDetails.CurrentRow != null)
        //    {
        //        PrintInfo p = dgvDetails.CurrentRow.DataBoundItem as PrintInfo;

        //        FrmSelectPrinter frm = new FrmSelectPrinter();
        //        frm.Text = "修改打印信息";
        //        frm.Tag = p;
        //        if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
        //        {
        //            string sql = EntityHelper.GetUpdateSql(p);
        //            string msg = string.Empty;

        //            if (DALOpr.ExecuteNonQuery(sql, ref msg))
        //            {
        //                int idx = details.IndexOf(p);
        //                details.RemoveAt(idx);
        //                details.Insert(idx, p);

        //                dgvDetails.SetRowSelect(idx);


        //                ShowInfoMessageBox("提示", "修改成功");
        //            }
        //            else
        //            {
        //                ShowErrorMessageBox("修改失败", msg);
        //            }

        //        }
        //    }
        //    else
        //    {
        //        ShowWarnningMessageBox("提示", "先选择修改的打印信息！");
        //    }
        //}

        //数据包一并更新
        private void toolBtnModify_Click(object sender, EventArgs e)
        {
            if (dgvDetails.CurrentRow != null)
            {
                PrintInfo p = dgvDetails.CurrentRow.DataBoundItem as PrintInfo;
                string pathOrg = p.CodePath;

                FrmSelectPrinter frm = new FrmSelectPrinter();
                frm.Text = "修改打印信息";
                frm.Tag = p;
                if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string sql = EntityHelper.GetUpdateSql(p);
                    string msg = string.Empty;

                    if (pathOrg.Equals(p.CodePath))
                    {//数据包没变
                        if (DALOpr.ExecuteNonQuery(sql, ref msg))
                        {
                            int idx = details.IndexOf(p);
                            details.RemoveAt(idx);
                            details.Insert(idx, p);

                            dgvDetails.SetRowSelect(idx);


                            ShowInfoMessageBox("提示", "修改成功");
                        }
                        else
                        {
                            ShowErrorMessageBox("修改失败", msg);
                        }
                    }
                    else
                    {
                        List<string> sqls = new List<string>();
                        sqls.Add(sql);

                        sql = "delete from PrintCode where PrintInfoID = " + p.ID;
                        sqls.Add(sql);

                        if (DALOpr.ExecuteSqlTran(sqls.ToArray(), ref msg))
                        {
                            if (File.Exists(p.CodePath))
                            {
                                string[] datas = File.ReadAllLines(p.CodePath, FileUtil.GetEncoding(p.CodePath));

                                //显示警告信息与启动任务的进度条冲突
                                if (datas.Length < nPrintLackQty)
                                {
                                    ShowErrorMessageBox("提示", string.Format("{0}号工位待打印数据已不足{1}条，剩余{2}条", p.StationNum, nPrintLackQty, datas.Length));
                                }
                                else
                                {
                                    //插入码明细
                                    DataTable dt = new DataTable();
                                    dt.Columns.Add("code", typeof(string));
                                    dt.Columns.Add("PrintInfoID", typeof(int));

                                    foreach (var item in datas)
                                    {
                                        DataRow dr = dt.NewRow();
                                        dr[0] = item;
                                        dr[1] = p.ID;

                                        dt.Rows.Add(dr);
                                    }

                                    if (DALOpr.ExcuteBulkCopy("PrintCode", dt, ref msg))
                                    {
                                        ShowInfoMessageBox("提示", "修改成功");
                                    }
                                    else
                                    {
                                        ShowErrorMessageBox("修改失败", msg);
                                    }
                                }
                            }
                            else
                            {

                                ShowErrorMessageBox("提示", string.Format("{0}号工位待打印数据文件不存在", p.StationNum));
                            }
                        }
                        else
                        {
                            ShowErrorMessageBox("修改失败", msg);
                        }
                    }


                }
            }
            else
            {
                ShowWarnningMessageBox("提示", "先选择修改的打印信息！");
            }
        }

        private void toolBtnDelete_Click(object sender, EventArgs e)
        {
            if (dgvDetails.CurrentRow != null)
            {
                PrintInfo p = dgvDetails.CurrentRow.DataBoundItem as PrintInfo;

                if (MessageBox.Show(this, "确认删除以下打印信息？\r\n" + p.ToString(), "请确认", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == System.Windows.Forms.DialogResult.Yes)
                {
                    string sql = "delete from PrintInfo where id = " + p.ID;
                    string msg = string.Empty;

                    if (DALOpr.ExecuteNonQuery(sql, ref msg))
                    {
                        details.Remove(p);

                        msg = string.Empty;
                        sql = "delete from PrintCode where PrintInfoID = " + p.ID;

                        DALOpr.ExecuteNonQuery(sql, ref msg);
                    }
                    else
                    {
                        ShowErrorMessageBox("删除失败", msg);
                    }
                }
            }
            else
            {
                ShowWarnningMessageBox("提示", "先选择删除的打印信息！");
            }
        }

        private void toolBtnAdd_Click(object sender, EventArgs e)
        {
            FrmSelectPrinter frm = new FrmSelectPrinter();
            frm.Text = "添加打印信息";
            if (frm.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                PrintInfo p = frm.Tag as PrintInfo;
                string msg = string.Empty;

                //if (details.Contains(p))
                //{
                //    ShowWarnningMessageBox("警告", "相同工位号和子位号不能重复添加打印机");
                //    return;
                //}

                p.TaskID = 0;

                if (File.Exists(p.CodePath))
                {
                    string[] datas = File.ReadAllLines(p.CodePath, FileUtil.GetEncoding(p.CodePath));

                    //显示警告信息与启动任务的进度条冲突
                    if (datas.Length  < nPrintLackQty)
                    {
                        ShowErrorMessageBox("提示", string.Format("{0}号工位待打印数据已不足{1}条，剩余{2}条", p.StationNum, nPrintLackQty, datas.Length));
                    }
                    else
                    {
                        if (DALOpr.Insert(p, ref msg))
                        {
                            details.Add(p);

                            dgvDetails.SetRowSelect(details.Count - 1);

                            //插入码明细
                            DataTable dt = new DataTable();
                            dt.Columns.Add("code", typeof(string));
                            dt.Columns.Add("PrintInfoID", typeof(int));

                            foreach (var item in datas)
                            {
                                DataRow dr = dt.NewRow();
                                dr[0] = item;
                                dr[1] = p.ID;

                                dt.Rows.Add(dr);
                            }

                            if (DALOpr.ExcuteBulkCopy("PrintCode", dt, ref msg))
                            {
                                ShowInfoMessageBox("提示", "添加成功");
                            }
                            else
                            {
                                ShowErrorMessageBox("添加失败", msg);
                            }
                        }
                        else
                        {
                            ShowErrorMessageBox("添加失败", msg);
                        }
                    }
                }
                else
                {

                    ShowErrorMessageBox("提示", string.Format("{0}号工位待打印数据文件不存在", p.StationNum));
                }

                
            }
        }

        private void dgvDetails_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            toolBtnModify.PerformClick();
        }
    }
}
